﻿using System;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using System.Xml;

namespace HYFamilyLibDataManager
{
	public class ConfigUtils
	{
		public static string GetDbConfigFileName()
		{
			string location = Assembly.GetExecutingAssembly().Location;
			int length = location.LastIndexOf("\\");
			string text = location.Substring(0, length);
			int length2 = text.LastIndexOf("\\");
			string text2 = text.Substring(0, length2) + "\\Data" + "\\DbConfig.xml";
			if (!new FileInfo(text2).Exists)
			{
				ConfigUtils.CreateFamilyLibXmlFile(text2);
			}
			return text2;
		}

		public static void CreateFamilyLibXmlFile(string fileName)
		{
			try
			{
				XmlDocument xmlDocument = new XmlDocument();
				XmlNode newChild = xmlDocument.CreateXmlDeclaration("1.0", "gb2312", null);
				xmlDocument.AppendChild(newChild);
				XmlElement xmlElement = xmlDocument.CreateElement("", "族库配置", "");
				XmlElement xmlElement2 = xmlDocument.CreateElement("Revit2012");
				XmlElement xmlElement3 = xmlDocument.CreateElement("Revit2013");
				XmlElement xmlElement4 = xmlDocument.CreateElement("Revit2014");
				XmlElement xmlElement5 = xmlDocument.CreateElement("Revit2015");
				XmlElement newChild2 = xmlDocument.CreateElement("Revit2016");
				XmlElement xmlElement6 = xmlDocument.CreateElement("族库类型");
				xmlElement6.SetAttribute("族库类型", "Local");
				XmlElement xmlElement7 = xmlDocument.CreateElement("本地族库数据");
				xmlElement7.SetAttribute("族库路径", "");
				XmlElement xmlElement8 = xmlDocument.CreateElement("用户族库数据");
				xmlElement8.SetAttribute("族库路径", "");
				XmlElement xmlElement9 = xmlDocument.CreateElement("服务器族库数据");
				xmlElement9.SetAttribute("服务器地址", "");
				xmlElement9.SetAttribute("用户名", "");
				xmlElement9.SetAttribute("密码", "");
				XmlElement xmlElement10 = xmlDocument.CreateElement("项目族库类型");
				xmlElement10.SetAttribute("项目族库类型", "LocalProject");
				XmlElement xmlElement11 = xmlDocument.CreateElement("本地项目族库数据");
				xmlElement11.SetAttribute("族库路径", "");
				XmlElement xmlElement12 = xmlDocument.CreateElement("企业族库数据");
				xmlElement12.SetAttribute("服务器地址", "");
				xmlElement12.SetAttribute("用户名", "");
				xmlElement12.SetAttribute("密码", "");
				xmlElement2.AppendChild(xmlElement6);
				xmlElement2.AppendChild(xmlElement7);
				xmlElement2.AppendChild(xmlElement8);
				xmlElement2.AppendChild(xmlElement9);
				xmlElement2.AppendChild(xmlElement10);
				xmlElement2.AppendChild(xmlElement11);
				xmlElement2.AppendChild(xmlElement12);
				XmlElement xmlElement13 = xmlDocument.CreateElement("族库类型");
				xmlElement13.SetAttribute("族库类型", "Local");
				XmlElement xmlElement14 = xmlDocument.CreateElement("本地族库数据");
				xmlElement14.SetAttribute("族库路径", "");
				XmlElement xmlElement15 = xmlDocument.CreateElement("用户族库数据");
				xmlElement15.SetAttribute("族库路径", "");
				XmlElement xmlElement16 = xmlDocument.CreateElement("服务器族库数据");
				xmlElement16.SetAttribute("服务器地址", "");
				xmlElement16.SetAttribute("用户名", "");
				xmlElement16.SetAttribute("密码", "");
				XmlElement xmlElement17 = xmlDocument.CreateElement("项目族库类型");
				xmlElement17.SetAttribute("项目族库类型", "LocalProject");
				XmlElement xmlElement18 = xmlDocument.CreateElement("本地项目族库数据");
				xmlElement18.SetAttribute("族库路径", "");
				XmlElement xmlElement19 = xmlDocument.CreateElement("企业族库数据");
				xmlElement19.SetAttribute("服务器地址", "");
				xmlElement19.SetAttribute("用户名", "");
				xmlElement19.SetAttribute("密码", "");
				xmlElement3.AppendChild(xmlElement13);
				xmlElement3.AppendChild(xmlElement14);
				xmlElement3.AppendChild(xmlElement15);
				xmlElement3.AppendChild(xmlElement16);
				xmlElement3.AppendChild(xmlElement17);
				xmlElement3.AppendChild(xmlElement18);
				xmlElement3.AppendChild(xmlElement19);
				XmlElement xmlElement20 = xmlDocument.CreateElement("族库类型");
				xmlElement20.SetAttribute("族库类型", "Local");
				XmlElement xmlElement21 = xmlDocument.CreateElement("本地族库数据");
				xmlElement21.SetAttribute("族库路径", "");
				XmlElement xmlElement22 = xmlDocument.CreateElement("用户族库数据");
				xmlElement22.SetAttribute("族库路径", "");
				XmlElement xmlElement23 = xmlDocument.CreateElement("服务器族库数据");
				xmlElement23.SetAttribute("服务器地址", "");
				xmlElement23.SetAttribute("用户名", "");
				xmlElement23.SetAttribute("密码", "");
				XmlElement xmlElement24 = xmlDocument.CreateElement("项目族库类型");
				xmlElement24.SetAttribute("项目族库类型", "LocalProject");
				XmlElement xmlElement25 = xmlDocument.CreateElement("本地项目族库数据");
				xmlElement25.SetAttribute("族库路径", "");
				XmlElement xmlElement26 = xmlDocument.CreateElement("企业族库数据");
				xmlElement26.SetAttribute("服务器地址", "");
				xmlElement26.SetAttribute("用户名", "");
				xmlElement26.SetAttribute("密码", "");
				xmlElement4.AppendChild(xmlElement20);
				xmlElement4.AppendChild(xmlElement21);
				xmlElement4.AppendChild(xmlElement22);
				xmlElement4.AppendChild(xmlElement23);
				xmlElement4.AppendChild(xmlElement24);
				xmlElement4.AppendChild(xmlElement25);
				xmlElement4.AppendChild(xmlElement26);
				XmlElement xmlElement27 = xmlDocument.CreateElement("族库类型");
				xmlElement27.SetAttribute("族库类型", "Local");
				XmlElement xmlElement28 = xmlDocument.CreateElement("本地族库数据");
				xmlElement28.SetAttribute("族库路径", "");
				XmlElement xmlElement29 = xmlDocument.CreateElement("用户族库数据");
				xmlElement29.SetAttribute("族库路径", "");
				XmlElement xmlElement30 = xmlDocument.CreateElement("服务器族库数据");
				xmlElement30.SetAttribute("服务器地址", "");
				xmlElement30.SetAttribute("用户名", "");
				xmlElement30.SetAttribute("密码", "");
				XmlElement xmlElement31 = xmlDocument.CreateElement("项目族库类型");
				xmlElement31.SetAttribute("项目族库类型", "LocalProject");
				XmlElement xmlElement32 = xmlDocument.CreateElement("本地项目族库数据");
				xmlElement32.SetAttribute("族库路径", "");
				XmlElement xmlElement33 = xmlDocument.CreateElement("企业族库数据");
				xmlElement33.SetAttribute("服务器地址", "");
				xmlElement33.SetAttribute("用户名", "");
				xmlElement33.SetAttribute("密码", "");
				xmlElement5.AppendChild(xmlElement27);
				xmlElement5.AppendChild(xmlElement28);
				xmlElement5.AppendChild(xmlElement29);
				xmlElement5.AppendChild(xmlElement30);
				xmlElement5.AppendChild(xmlElement31);
				xmlElement5.AppendChild(xmlElement32);
				xmlElement5.AppendChild(xmlElement33);
				XmlElement xmlElement34 = xmlDocument.CreateElement("族库类型");
				xmlElement34.SetAttribute("族库类型", "Local");
				XmlElement xmlElement35 = xmlDocument.CreateElement("本地族库数据");
				xmlElement35.SetAttribute("族库路径", "");
				XmlElement xmlElement36 = xmlDocument.CreateElement("用户族库数据");
				xmlElement36.SetAttribute("族库路径", "");
				XmlElement xmlElement37 = xmlDocument.CreateElement("服务器族库数据");
				xmlElement37.SetAttribute("服务器地址", "");
				xmlElement37.SetAttribute("用户名", "");
				xmlElement37.SetAttribute("密码", "");
				XmlElement xmlElement38 = xmlDocument.CreateElement("项目族库类型");
				xmlElement38.SetAttribute("项目族库类型", "LocalProject");
				XmlElement xmlElement39 = xmlDocument.CreateElement("本地项目族库数据");
				xmlElement39.SetAttribute("族库路径", "");
				XmlElement xmlElement40 = xmlDocument.CreateElement("企业族库数据");
				xmlElement40.SetAttribute("服务器地址", "");
				xmlElement40.SetAttribute("用户名", "");
				xmlElement40.SetAttribute("密码", "");
				xmlElement5.AppendChild(xmlElement34);
				xmlElement5.AppendChild(xmlElement35);
				xmlElement5.AppendChild(xmlElement36);
				xmlElement5.AppendChild(xmlElement37);
				xmlElement5.AppendChild(xmlElement38);
				xmlElement5.AppendChild(xmlElement39);
				xmlElement5.AppendChild(xmlElement40);
				xmlElement.AppendChild(xmlElement2);
				xmlElement.AppendChild(xmlElement3);
				xmlElement.AppendChild(xmlElement4);
				xmlElement.AppendChild(xmlElement5);
				xmlElement.AppendChild(newChild2);
				xmlDocument.AppendChild(xmlElement);
				xmlDocument.Save(fileName);
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}

		public static void WriteFamilyLibXmlFile(string fileName, string version, string dbTypeStr, DbData data)
		{
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(fileName);
			XmlNode xmlNode = xmlDocument.SelectSingleNode("族库配置");
			XmlNode xmlNode2 = null;
			foreach (object obj in xmlNode.ChildNodes)
			{
				XmlNode xmlNode3 = (XmlNode)obj;
				if (xmlNode3.Name.Trim().ToLower().Equals("revit" + version))
				{
					xmlNode2 = xmlNode3;
				}
			}
			if (dbTypeStr == "族库类型")
			{
				xmlNode2.ChildNodes[0].Attributes[dbTypeStr].Value = data.DatabaseType.ToString();
			}
			else if (dbTypeStr == "项目族库类型")
			{
				xmlNode2.ChildNodes[4].Attributes[dbTypeStr].Value = data.DatabaseType.ToString();
			}
			if (data is UserDbData)
			{
				xmlNode2.ChildNodes[2].Attributes["族库路径"].Value = ((UserDbData)data).DatabasePath;
			}
			else if (data is ServerDbData)
			{
				XmlNode xmlNode4 = xmlNode2.ChildNodes[3];
				xmlNode4.Attributes["服务器地址"].Value = ((ServerDbData)data).ConnectData.IP;
				xmlNode4.Attributes["用户名"].Value = ((ServerDbData)data).ConnectData.UserName;
				xmlNode4.Attributes["密码"].Value = ((ServerDbData)data).ConnectData.Password;
			}
			else if (data is CompanyDbData)
			{
				XmlNode xmlNode5 = xmlNode2.ChildNodes[6];
				xmlNode5.Attributes["服务器地址"].Value = ((CompanyDbData)data).ConnectData.IP;
				xmlNode5.Attributes["用户名"].Value = ((CompanyDbData)data).ConnectData.UserName;
				xmlNode5.Attributes["密码"].Value = ((CompanyDbData)data).ConnectData.Password;
			}
			else if (data is LocalDbData)
			{
				xmlNode2.ChildNodes[1].Attributes["族库路径"].Value = ((LocalDbData)data).DatabasePath;
			}
			else if (data is LocalProjectDbData)
			{
				xmlNode2.ChildNodes[5].Attributes["族库路径"].Value = ((LocalProjectDbData)data).DatabasePath;
			}
			xmlDocument.Save(fileName);
		}

		public static DbData ReadFamilyLibXmlFile(string DbType, string version)
		{
			string dbConfigFileName = ConfigUtils.GetDbConfigFileName();
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(dbConfigFileName);
			XmlNode xmlNode = xmlDocument.SelectSingleNode("族库配置");
			XmlNode xmlNode2 = null;
			foreach (object obj in xmlNode.ChildNodes)
			{
				XmlNode xmlNode3 = (XmlNode)obj;
				if (xmlNode3.Name.Trim().ToLower().Equals("revit" + version))
				{
					xmlNode2 = xmlNode3;
				}
			}
			if (xmlNode2 == null)
			{
				return null;
			}
			if (DbType == "族库类型")
			{
				string text = xmlNode2.ChildNodes[0].Attributes[DbType].Value.Trim();
				if (text.Equals("Local"))
				{
					return new LocalDbData(xmlNode2.ChildNodes[1].Attributes["族库路径"].Value);
				}
				if (text.Equals("User"))
				{
					return new UserDbData(xmlNode2.ChildNodes[2].Attributes["族库路径"].Value);
				}
				if (text.Equals("Server"))
				{
					XmlNode xmlNode4 = xmlNode2.ChildNodes[3];
					return new ServerDbData(xmlNode4.Attributes["服务器地址"].Value, xmlNode4.Attributes["用户名"].Value, xmlNode4.Attributes["密码"].Value);
				}
				if (text.Equals("Company"))
				{
					XmlNode xmlNode5 = xmlNode2.ChildNodes[6];
					return new CompanyDbData(xmlNode5.Attributes["服务器地址"].Value, xmlNode5.Attributes["用户名"].Value, xmlNode5.Attributes["密码"].Value);
				}
				return null;
			}
			else
			{
				if (!(DbType == "项目族库类型"))
				{
					return null;
				}
				string text2 = xmlNode2.ChildNodes[4].Attributes[DbType].Value.Trim();
				if (text2.Equals("Server"))
				{
					XmlNode xmlNode6 = xmlNode2.ChildNodes[3];
					return new ServerDbData(xmlNode6.Attributes["服务器地址"].Value, xmlNode6.Attributes["用户名"].Value, xmlNode6.Attributes["密码"].Value);
				}
				if (text2.Equals("LocalProject"))
				{
					return new LocalDbData(xmlNode2.ChildNodes[5].Attributes["族库路径"].Value);
				}
				return null;
			}
		}

		public static DbData ReadUserData(string DbType, string version)
		{
			string dbConfigFileName = ConfigUtils.GetDbConfigFileName();
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(dbConfigFileName);
			XmlNode xmlNode = xmlDocument.SelectSingleNode("族库配置");
			XmlNode xmlNode2 = null;
			foreach (object obj in xmlNode.ChildNodes)
			{
				XmlNode xmlNode3 = (XmlNode)obj;
				if (xmlNode3.Name.Trim().ToLower().Equals("revit" + version))
				{
					xmlNode2 = xmlNode3;
				}
			}
			if (DbType == "族库类型")
			{
				return new UserDbData(xmlNode2.ChildNodes[2].Attributes["族库路径"].Value);
			}
			return null;
		}
	}
}
